package com.autonavi.yunda.yunji.core.model.mappers;

import com.autonavi.yunda.yunji.core.model.dto.DatasourceConfigDTO;
import com.autonavi.yunda.yunji.core.model.dto.DatasourceProjectMappingDTO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;


/**
 * @author sunzhenbang
 */
@Mapper

public interface DatasourceProjectMappingMapper extends BaseMapper<DatasourceProjectMappingDTO> {
    @SuppressWarnings("all")
    Integer insertBatchSomeColumn(@Param("list") List<DatasourceProjectMappingDTO> datasourceProjectMappingDTOList);

    @Delete("delete from yunji_datasource_project_mapping where datasource_id = #{datasourceId}")
    int deleteByDatasourceId(@Param("datasourceId") Long datasourceId);
    @Select("select datasource_id from yunji_datasource_project_mapping where project_id = #{projectId}")
    List<String> listDatasourceIdsByPid(@Param("projectId") String projectId);
    @Select({"<script>",
            " SELECT datasource_id, project_id FROM yunji_datasource_project_mapping WHERE project_id in ",
            "<foreach item='item' index='index' collection='projectIds' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<DatasourceProjectMappingDTO> listByProjectIds(@Param("projectIds") List<String> projectIds);

    /**
     * 根据项目id查询完整数据源
     * @param projectIds    项目id
     * @return  符合条件的完整数据源内容
     */
    @Select({"<script>",
            " SELECT distinct con.* ",
            " FROM yunji_datasource_project_mapping as map, yunji_datasource_config as con ",
            " WHERE 1=1 ",
            " and map.datasource_id = con.id ",
            " and con.is_deleted = 0 ",
            " <if test='projectIds != null and projectIds.size > 0' >",
            "   and map.project_id in ",
            "   <foreach item='item' index='index' collection='projectIds' open='(' separator=',' close=')'>",
            "       #{item}",
            "   </foreach> ",
            " </if> ",
            "</script>"})
    List<DatasourceConfigDTO> listDatasourceByProjectIds(List<String> projectIds);
}
